import {Button, message, Upload, UploadProps} from "antd";
import {aliyunUploadFile} from "@/services/admin-api/aliyun";
import {UploadOutlined} from "@ant-design/icons";
import React from "react";

export default (props: any) => {

  console.log('props = ', props)

  const {objId, objIdStr, objType, back} = props

  const propsUpload: UploadProps = {
    accept: "image/png, image/jpeg, image/jpg",
    beforeUpload: async (file) => {
      const isPNG = file.type === 'image/png' || file.type === 'image/jpeg' || file.type === 'image/jpg';
      if (!isPNG) {
        message.error(`${file.name} is not a png file`);
        return false
      }

      await aliyunUploadFile(file, objId, objIdStr, objType)
      back()
      return false;
    },
  };

  return <Upload {...propsUpload}>
    <Button size={'small'} icon={<UploadOutlined/>}/>
  </Upload>
}
